﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace Prototype1.__Membership
{
    public partial class WF_ManageProfile : System.Web.UI.Page
    {
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            String userQS = Context.Request.QueryString["user"];
            Profile p = null;

            //TESTING
            userQS = "00000000-0000-0000-0000-000000000000";
            if (userQS != null)
            {
                Guid x = new Guid(userQS);

                this.hiddenGUID.Value = userQS;

                using (DataAccessDataContext dc = new DataAccessDataContext())
                {
                    var query = from pfile in dc.Profiles
                                where pfile.email.Equals(x)
                                select pfile;

                    p = query.Single<Profile>(); //Não dá para testar para ver se tem?? :X n devolve null
                }

                if (p != null)
                {
                    this.FullName.Text = p.FullName;
                    this.Morada.Text = p.Morada;
                    this.NIB.Text = p.NIB;
                    this.Telefone.Text = "" + p.Telefone;
                    this.Telemovel.Text = "" + p.Telemovel;
                }
            }

        }

        protected void save_profile(object sender, EventArgs e)
        {
            Profile p = new Profile();
            if (this.FullName.Text != null)p.FullName = this.FullName.Text;
            if (this.Morada.Text != null) p.Morada = this.Morada.Text;
            if (this.NIB.Text != null) p.NIB = this.NIB.Text;
            if (this.Telefone.Text != null) p.Telefone = int.Parse(this.Telefone.Text);
            if (this.Telemovel.Text != null) p.Telemovel = int.Parse(this.Telemovel.Text);

            using (DataAccessDataContext dc = new DataAccessDataContext())
            {                
                var prfile = dc.Profiles.Single(px => px.email.Equals(this.hiddenGUID.Value)); 
                               
                prfile.FullName = p.FullName;
                prfile.Morada = p.Morada;
                prfile.NIB = p.NIB;
                prfile.Telefone = p.Telefone;
                prfile.Telemovel = p.Telemovel;
                
                

                dc.SubmitChanges();
                this.Status.Text = "Updated!";
            }
        }
    }
}
